package com.zhsj.OkeHomeConsumption.adapter;


import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhsj.OkeHomeConsumption.R;
import com.zhsj.OkeHomeConsumption.empty.MerchantTypeBean;

import java.util.ArrayList;


/**
 * <p>文件描述：我的  中间icon 适配器<p>
 * <p>作者：xingdx<p>
 * <p>创建时间：2018/8/11<p>
 * <p>更改时间：2018/8/11<p>
 */
public class PersonCenterAdapter extends RecyclerView.Adapter<PersonCenterAdapter.ViewHolder> {
    private Context mContext;
    private OnItemClickLitener mOnItemClickLitener;
    private ArrayList<MerchantTypeBean> mMerchantTypeLists = null;

    public PersonCenterAdapter(Context context,ArrayList<MerchantTypeBean> mMerchantTypeLists ){
       this.mMerchantTypeLists = mMerchantTypeLists;
        this.mContext = context;
    }

    public interface OnItemClickLitener {

        void GoItemInfo(int position);
    }

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public PersonCenterAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.index_center_item_view, parent, false);
        return new ViewHolder(v);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.showData( position);
    }

    @Override
    public int getItemCount() {
        return mMerchantTypeLists.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_title;
        private ImageView iv_icon;
        private RelativeLayout rv_index;
        public ViewHolder(View itemView) {
            super(itemView);

            findView(itemView);
        }

        private void findView(View itemView) {
            tv_title = (TextView)itemView.findViewById(R.id.tv_title);
            iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon);
            rv_index = (RelativeLayout)itemView.findViewById(R.id.rv_index);
        }

        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
        public void showData(final int position) {
            rv_index.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null!=mOnItemClickLitener){
                        mOnItemClickLitener.GoItemInfo(position);
                    }
                }
            });
            Glide.with(mContext).load(mMerchantTypeLists.get(position).getCategoryIcon()).placeholder(R.drawable.zhanweitu_bg).into(iv_icon);
            tv_title.setText( mMerchantTypeLists.get(position).getName());
//            iv_icon.setBackground(mContext.getResources().getDrawable(R.mipmap.canyinmeishi));

        }
    }
}
